package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.mapper.EduVideoMapper;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.eduservice.service.client.VodClient;
import com.atguigu.servicebase.exceptionhandler.CustomizeException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author hwt
 * @since 2022-08-23
 */
@Service
public class EduVideoServiceImpl extends ServiceImpl<EduVideoMapper, EduVideo> implements EduVideoService {

    @Resource
    VodClient vodClient;

    @Override
    public void removeVideoByCourseId(String courseId) {
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);

        List<EduVideo> eduVideos = baseMapper.selectList(wrapper);

        List<String> ids = new ArrayList<>();

        for (EduVideo video : eduVideos) {
            if (!video.getVideoSourceId().isEmpty()){
                ids.add(video.getVideoSourceId());
            }
        }
        try {
            // 如果远程调用时，出现异常了，这里也直接返回错误信息
            if (!ids.isEmpty()) vodClient.deleteBatch(ids);

        }catch (Exception e){
            throw new  CustomizeException(500,"课程删除失败");
        }
        baseMapper.delete(wrapper);
    }
}
